import threading, Queue, random, thread, time
from math import *
import human_control, agent_controller, graphics_engine, world_model
import soccer_game_model

# Utility classes used to represent graphics
class agent_graphics(object):
	def __init__(self,graphics_engine):
		self.graphics_engine=graphics_engine
	def agent_graphics_draw(self,object_num,pos,heading,graphics_state,im_file):
		self.graphics_engine.update_pos(object_num,pos,heading,graphics_state,im_file)
	def agent_graphics_die(self,object_num):
		self.graphics_engine.remove_agent(object_num)

class wall_graphics(object):
	def __init__(self,graphics_engine):
		self.graphics_engine=graphics_engine
	def wall_draw(self,points,width):
		self.graphics_engine.create_walls([points],width)
	def wall_die(self):
		return
		
class text_graphics(object):
	def __init__(self,graphics_engine):
		self.graphics_engine = graphics_engine
	def text_draw(self, text_num, pos, text):
		self.graphics_engine.update_text(text_num, pos, text)
	def text_die(self, text_num):
		self.graphics_engine.remove_text(text_num)


# Initialize world, game, IO, control_queue,etc
control_queue=Queue.Queue()
main_graphics_engine=graphics_engine.graphics_engine(height=720,width=720)
main_graphics_engine.start_event_loop()
main_world_model=world_model.world_model()
a_graphics=agent_graphics(main_graphics_engine)
w_graphics=wall_graphics(main_graphics_engine)
t_graphics=text_graphics(main_graphics_engine)
	
soccer_game_model=soccer_game_model.soccer_game_model(main_world_model,w_graphics,a_graphics,t_graphics)
main_agent_controller=agent_controller.agent_controller(main_world_model.objects)
main_human_control=human_control.human_control(graphics_engine= main_graphics_engine,control_queue=control_queue,agent_controller=main_agent_controller,	world_model=main_world_model, game_model=soccer_game_model, t_graphics=t_graphics)

# This is the main thread's processing loop
while main_graphics_engine.mainloop_thread.isAlive():
	# Process human input events
	try:
		temp_fun=control_queue.get(block=False)
		temp_fun()
	except Queue.Empty:
		time.sleep(.01)
		if main_human_control.game_active:
			soccer_game_model.think()
soccer_game_model.quit()
main_graphics_engine.mainloop_thread.join()
